import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:ichinesereader/constants.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/register/paymentRecordDto.dart';
import 'package:ichinesereader/utils/log.dart';

class RegisterServiceProvider extends GetConnect {
  var box = Hive.box('appconfig');
  RegisterServiceProvider() {
    httpClient.addResponseModifier((request, response) {
      if (response.statusCode == 403) {
        Get.snackbar(
            'Message'.tr,
            'You have been logged out due to inactivity or security reasons.'
                .tr,
            backgroundColor: Color(0xff23c6c8),
            colorText: Color(0xffd8ffff),
            snackPosition: SnackPosition.BOTTOM);
        return Get.offAllNamed('/login');
      } else if (response.statusCode == 401) {
        Get.snackbar('Message'.tr, 'Unauthorized access'.tr,
            backgroundColor: Color(0xff23c6c8),
            colorText: Color(0xffd8ffff),
            snackPosition: SnackPosition.BOTTOM);
        return Get.offAllNamed('/login');
      } else {
        Log.debug(response.request?.url.toString());
        return response;
      }
    });
    httpClient.maxAuthRetries = 2;
    httpClient.timeout = Duration(seconds: 30);
  }
  // for ICR
  Future<dynamic> getPaymentToken(userInfo) async {
    //var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await post(
      '${Constants.BaseAPI}usermanager/payment/get_token',
      //box.get('dataForPay').toString(),
      userInfo,
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        // "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<dynamic> createIndividual(userInfo, isSeparateFromSchool) async {
    final response = await post(
      '${Constants.BaseAPI}usermanager/createIndividual/$isSeparateFromSchool',
      userInfo,
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
      },
    );
    return response;
  }

  Future<dynamic> getPaymentRecordICRList() async {
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await get(
      '${Constants.BaseAPI}usermanager/payment',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<dynamic> getPaymentPlans() async {
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await get(
      '${Constants.BaseAPI}usermanager/payment/plans',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<dynamic> checkPaymentQuote(String quoteNumber, String email) async {
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await get(
      '${Constants.BaseAPI}usermanager/payment/quoteCheck',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<dynamic> cancelSubscription() async {
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await post(
      '${Constants.BaseAPI}usermanager/payment/cancelEx',
      {},
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        "authtoken": _authtoken
      },
    );
    return response;
  }
  // Future<dynamic> triggerBrainTreePayment() async {
  //   var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
  //   final response = await post(
  //     '${Constants.BaseAPI}usermanager/payment/get_token',
  //     box.get('dataForPay'),
  //     headers: <String, String>{
  //       'content-type': 'application/json;charset=UTF-8',
  //       "authtoken": _authtoken
  //     },
  //   );
  //   return response;
  // }

  // for ICA
  // for order history
  Future<dynamic> getPaymentRecordListICA() async {
    var _userName = box.get(CacheManagerKey['username'].toString());
    final response = await get(
      '${Constants.BaseAPIICA}payment/record?userName=$_userName',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
      },
    );
    return response;
  }

  // for ICA
  Future<dynamic> getPaymentTokenICA(userInfo) async {
    final response = await post(
      '${Constants.BaseAPIICA}payment/token',
      userInfo,
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
      },
    );
    return response;
  }

  // for purchase
  Future<dynamic> getPaymentRecordICA(userInfo) async {
    final response = await post(
      '${Constants.BaseAPIICA}payment/record',
      userInfo,
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
      },
    );
    return response;
  }

  Future<dynamic> createIndividualICA(userInfo, isSeparateFromSchool) async {
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await post(
      '${Constants.BaseAPIICA}createPlayer',
      userInfo,
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<dynamic> calculatePrice(studentNumber, paymentTerm, zipCode) async {
    //var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await get(
      '${Constants.BaseAPI}usermanager/payment/calculatePrice?studentNumber=$studentNumber&paymentTerm=$paymentTerm&zipCode=$zipCode&platform=reader',
      //box.get('dataForPay').toString(),
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
        // "authtoken": _authtoken
      },
    );
    return response;
  }

  Future<bool> isEmailAvailable(String email) async {
    String url = '${Constants.BaseAPI}usermanager/available/email/$email';
    final response = await get(url);
    return response.body;
  }

  Future<dynamic> createPaymentRecord(PaymentRecordDto dto) async {
    final response = await post(
      '${Constants.BaseAPI}usermanager/payment/record',
      dto.toJson(),
      decoder: (data) {
        return data;
      },
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getPaymentRecord(String recordId) async {
    final response = await get(
      '${Constants.BaseAPI}usermanager/payment/record?recordId=$recordId',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getProficiencyLevel() async {
    final response = await get(
      '${Constants.BaseAPISuper}superadmin/level?detail=true',
      headers: <String, String>{
        'content-type': 'application/json;charset=UTF-8',
      },
    );
    return response;
  }
}
